from django.db import models
import datetime
#python manage.py makemigrations common 更新数据库定义
#python manage.py migrate

class Customer(models.Model):
    # 客户名称
    name = models.CharField(max_length=200)

    # 联系电话
    phonenumber = models.CharField(max_length=200)

    # 地址
    address = models.CharField(max_length=200)

class Medicine(models.Model):
    name = models.CharField(max_length=200)
    #药品编号
    sn = models.CharField(max_length=200)
    desc = models.CharField(max_length=200)

class Order(models.Model):
    #订单名
    name = models.CharField(max_length=200, null=True, blank=True)
    #创建日期
    create_date = models.DateTimeField(default=datetime.datetime.now)
    #客户id
    #on_delete指定当被参照表发生改动时，此表如何反应 CASEDE/PROTECT/SET_NULL
    customer = models.ForeignKey(Customer, on_delete=models.PROTECT)
    #两个表形成多对多的关系
    medicines = models.ManyToManyField(Medicine, through='OrderMedicine')

class OrderMedicine(models.Model):
    order = models.ForeignKey(Order, on_delete=models.PROTECT)
    medicine = models.ForeignKey(Medicine, on_delete=models.PROTECT)
    amount = models.PositiveIntegerField()
from django.contrib import admin
admin.site.register(Customer)
